//
//  MainTabBar.swift
//  HDCWeibo
//
//  Created by 黄德超 on 16/4/7.
//  Copyright © 2016年 黄德超. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

   //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        setupUI()
        //fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加撰写按钮
        addSubview(composeBtn)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //手动修改所有 按钮 的位置
        //遍历所有的子视图
        let w = self.bounds.size.width / 5
        let h = self.bounds.size.height
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义递增变量
        var index: CGFloat = 0
        for subview in subviews {
           if
            subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            //修改frame
            subview.frame = CGRectOffset(rect, index * w, 0)
            
            index += index == 1 ? 2 : 1
            }
        }
        
        //设置撰写按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeBtn)
        
        
    }
    
    //MARK: 懒加载 撰写按钮
    lazy var composeBtn: UIButton = {
        //UIButton() 自定义样式按钮
        let btn = UIButton()
        
        //设置按钮
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return btn
    }()

}
